package com.example.dollarShipping.controller;


import com.example.dollarShipping.entity.*;
import com.example.dollarShipping.service.CarrierService;
import com.example.dollarShipping.service.UserService;
import com.example.dollarShipping.utils.Result;
import com.example.dollarShipping.utils.ResultCode;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin(origins = "*", maxAge = 3600)
@RestController()
@RequestMapping("/carrier")
public class CarrierController {

    @Resource
    CarrierService carrierService;
    @Resource
    UserService userService;
    @Resource
    PasswordEncoder passwordEncoder;

    ResultCode resultCode = new ResultCode();
    //根据承运商id得到承运商信息和货车列表、司机列表
    @GetMapping("/getCarrier")
    @PreAuthorize("hasAnyAuthority('admin','account','carrier','driver')")
    public Result findCarrier(@RequestParam(required = false,name = "id") Long id){
        Result res = new Result();
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            Carrier c = carrierService.findCarrier(id);
            map.put("carrierInfo",c);
            res.setMessage("返回成功");
            res.setData(map);
        } catch (Exception e) {
            e.printStackTrace();
            res.setCode(resultCode.InfoGetERR);
            res.setMessage(resultCode.getMsg(resultCode.InfoGetERR));
        }
        return res;
    }

    //承运商列表
    @GetMapping("/carrierList")
    @PreAuthorize("hasAnyAuthority('admin','account','carrier')")
    public Result getCarrierList(@RequestParam(required = false,name = "name") String name){
        Result res = new Result();
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            List<Carrier> list = carrierService.getInfo(name);
            map.put("carrierList",list);
            res.setMessage("返回成功");
            res.setData(map);
        } catch (Exception e) {
            e.printStackTrace();
            res.setCode(resultCode.InfoGetERR);
            res.setMessage(resultCode.getMsg(resultCode.InfoGetERR));
        }
        return res;
    }


    // 更新承运商信息
    @PutMapping("/modCarrier")
    @PreAuthorize("hasAnyAuthority('admin','carrier')")
    public Result modCarrier(@RequestBody Carrier c){
        Result res = new Result();
        try {
            int r = carrierService.update(c);
            if(r > 0){
                res.setMessage("信息更新成功");
            }else{
                res.setCode(ResultCode.InfoModERR);
                res.setMessage(resultCode.getMsg(ResultCode.InfoModERR));
            }
        } catch (Exception e) {
            e.printStackTrace();
            res.setCode(ResultCode.InfoModERR);
            res.setMessage(resultCode.getMsg(ResultCode.InfoModERR));
        }
        return res;
    }

    //有空的司机列表
    @GetMapping("/driverList")
    @PreAuthorize("hasAnyAuthority('carrier')")
    public Result getFreeDriver(@RequestParam(required = false,name = "id") Long id){
        Result res = new Result();
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            List<User> list = carrierService.findFreeDrivers(id);
            map.put("driverList",list);
            res.setMessage("返回有空闲的司机列表");
            res.setData(map);
        } catch (Exception e) {
            e.printStackTrace();
            res.setCode(resultCode.InfoGetERR);
            res.setMessage(resultCode.getMsg(resultCode.InfoGetERR));
        }
        return res;
    }

    //有空的货车列表
    @GetMapping("/truckList")
    @PreAuthorize("hasAnyAuthority('carrier')")
    public Result getFreeTruck(@RequestParam(required = false,name = "id") Long id){
        Result res = new Result();
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            List<Truck> list = carrierService.findFreeTrucks(id);
            map.put("driverList",list);
            res.setMessage("返回有空闲的货车");
            res.setData(map);
        } catch (Exception e) {
            e.printStackTrace();
            res.setCode(resultCode.InfoGetERR);
            res.setMessage(resultCode.getMsg(resultCode.InfoGetERR));
        }
        return res;
    }

    @PostMapping("/addDriver")
    @PreAuthorize("hasAnyAuthority('admin','carrier')")
    public Result addDriver(@Validated @RequestBody Map<String, Object> params){
        Long carrierId = Long.parseLong(params.get("carrierId").toString());
        String userName = (String) params.get("userName");
        String realName = (String) params.get("realName");
        String password = (String) params.get("password");
        password = passwordEncoder.encode(password);
        String phone = (String) params.get("phone");
        String email = (String) params.get("email");
        System.out.println("addDriver");
        Result res = new Result();
        try {
            userService.addDriver(carrierId,userName,realName,password,4,phone,email);
            res.setMessage("添加司机成功");
        } catch (Exception e) {
            e.printStackTrace();
            res.setCode(resultCode.InfoAddERR);
            res.setMessage(resultCode.getMsg(resultCode.InfoAddERR));
        }
        return res;
    }

    @PostMapping("/addTruck")
    @PreAuthorize("hasAnyAuthority('admin','carrier')")
    public Result addTruck(@RequestBody Truck t){
        System.out.println("addTruck");
        Result res = new Result();
        try {
            carrierService.addTruck(t);
            res.setMessage("新增货车成功");
        } catch (Exception e) {
            e.printStackTrace();
            res.setCode(resultCode.InfoAddERR);
            res.setMessage(resultCode.getMsg(resultCode.InfoAddERR));
        }
        return res;
    }

    //创建派车单
    @PostMapping("/createTruckOrder")
    @PreAuthorize("hasAnyAuthority('admin','carrier')")
    public Result createTruckOrder(@RequestBody TruckOrder t){
        Result res = new Result();
        try {
            carrierService.createTruckOrder(t);
            res.setMessage("创建货运单成功");
        } catch (Exception e) {
            e.printStackTrace();
            res.setCode(resultCode.InfoAddERR);
            res.setMessage(resultCode.getMsg(resultCode.InfoAddERR));
        }

        return res;
    }

    @GetMapping("/shippingOrderList")
    @PreAuthorize("hasAnyAuthority('carrier')")
    public Result getShippingOrderList(@RequestParam Map<String, Object> params){
        Integer state = null;
        Long carrierId = null;
        if(params.get("state")!=null)
            state = Integer.parseInt(params.get("state").toString());
        if(params.get("carrierId")!=null)
            carrierId = Long.parseLong(params.get("carrierId").toString());
        Result res = new Result();
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            List<ShippingOrder> list = carrierService.getShippingOrder(state,carrierId);
            map.put("shippingOrderList",list);
            res.setMessage("货运单返回成功");
            res.setData(map);
        } catch (Exception e) {
            e.printStackTrace();
            res.setCode(resultCode.InfoGetERR);
            res.setMessage(resultCode.getMsg(resultCode.InfoGetERR));
        }
        return res;
    }

    //查看货运单详情
    @GetMapping("/shippingOrderDetail")
    @PreAuthorize("hasAnyAuthority('admin','carrier')")
    public Result shippingOrderDetail(@RequestParam(required = false,name = "id") Long id){
        Result res = new Result();
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            map.put("shippingOrder",carrierService.findShippingOrder(id));
            res.setMessage("返回货运单详情");
            res.setData(map);
        } catch (Exception e) {
            e.printStackTrace();
            res.setCode(resultCode.InfoGetERR);
            res.setMessage(resultCode.getMsg(resultCode.InfoGetERR));
        }

        return res;
    }

    //接单货运单
    @PostMapping("/takeOrder")
    @PreAuthorize("hasAnyAuthority('carrier')")
    public Result takeOrder(@Validated @RequestBody Map<String,Object> params) {
        System.out.println("carrierOperate");
        Long carrierId = Long.parseLong(params.get("carrierId").toString());
        Long orderId = Long.parseLong(params.get("orderId").toString());
        Long operator = Long.parseLong(params.get("operator").toString());
        Result res = new Result();
        try {
            carrierService.changeState(carrierId,orderId,operator);
            res.setMessage("接单成功");
        } catch (Exception e) {
            e.printStackTrace();
            res.setCode(resultCode.InfoModERR);
            res.setMessage(resultCode.getMsg(resultCode.InfoModERR));
        }

        return res;
    }



}
